﻿(function ($) {
    function setValue(target, value, reg) {
        var transform = target.attr("transform");
        if (transform) {
            if (transform.match(reg))
                target.attr("transform", transform.replace(reg, value));
            else
                target.attr("transform", transform + " " + value);
        }
        else
            target.attr("transform", value);
    }

    function getValue(target, reg) {
        var nums = [];
        var transform = target.attr("transform");
        if (transform) {
            var ii = transform.match(reg);
            if (ii && ii.length > 0) {
                var d = ii[0].match(numberReg);
                for (var i = 0; i < d.length; d++) {
                    nums.push(parseFloat(d[i]));
                }
            }
        }

        return nums;
    }
    // /[-+]?\d*\.?\d+[e[+-]?\d+]?/g;
    var numberReg = /-?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
    var translateReg = /translate\s*\([^\(\)]+\)/i;

    $.fn.getTranslate = function () {
        var target = $(this);

        var transform = target.attr("transform");
        if (transform) {
            var ii = transform.match(translateReg);
            if (ii && ii.length > 0) {
                var d = ii[0].match(numberReg);
                if (d.length >= 2)
                    return { x: parseFloat(d[0]), y: parseFloat(d[1]) };
            }
        }
        return { x: 0, y: 0 };
    };

    $.fn.setTranslate = function (x, y) {
        setValue($(this), "translate(" + x + "," + y + ")", translateReg);
    };


    var rotateReg = /rotate\s*\([^\(\)]+\)/i;
    $.fn.setRotate = function (angle, x, y) {
        x |= 0;
        y |= 0;

        setValue($(this), "rotate(" + angle + "," + x + "," + y + ")", rotateReg);
    };

    $.fn.getRotate = function () {
        var nums = getValue($(this), rotateReg);
        if (nums.length > 0)
            return nums[0];
    }

    var scaleReg = /scale\s*\(\s*[-+]?\d*\.?\d+\s*\)/i;
    $.fn.setScale = function (x) {
        setValue($(this), "scale(" + x + ")", scaleReg);
    };


    $.fn.getScale = function () {
        var target = $(this);
        var transform = target.attr("transform");
        if (transform) {
            var ii = transform.match(scaleReg);
            if (ii && ii.length > 0) {
                var d = ii[0].match(numberReg);
                if (d.length >= 1)
                    return parseFloat(d[0]);
            }
        }
    };

    $.fn.svgAddClass = function (className) {
        var cla = $(this).attr("class");
        if (cla) {
            var claList = cla.split(" ");
            for (var i = 0; i < claList.length; i++) {
                if (claList[i] == className)
                    return;
            }
            $(this).attr("class", cla + " " + className);
        }
        else {
            $(this).attr("class", className);
        }
    };

    $.fn.svgRemoveClass = function (className) {
        var cla = $(this).attr("class");
        if (cla) {
            var claList = cla.split(" ");
            for (var i = 0; i < claList.length; i++) {
                if (claList[i] == className) {
                    claList.splice(i, 1);
                    break;
                }
            }

            $(this).attr("class", claList.join(" "));
        }
    };
})(jQuery);

function makeSVG(tag, attrs) {
    var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
    for (var k in attrs) {
        if (k == "href")
            el.setAttributeNS('http://www.w3.org/1999/xlink', 'href', attrs[k]);
        else
            el.setAttribute(k, attrs[k]);
    }

    return el;
}

function convertStringToSvg(txt) {
    function makeChildNode(c) {


        var tag = c.tagName;
        var attrs = new Object();

        var attrl = c.attributes.length;
        for (j = 0; j < attrl; j++) {
            attrs[c.attributes[j].nodeName] = c.attributes[j].nodeValue;
        }

        var root = $(makeSVG(tag, attrs));

        if (c.childNodes) {
            var childrenLength = c.childNodes.length;
            for (var i = 0; i < childrenLength; i++) {
                var child = c.childNodes[i];
                if (child.nodeType == 1)
                    root.append(makeChildNode(child));
            }
        }
        return root;
    }

    var xmlDoc = $.parseXML(txt);
    var root = null;

    if (xmlDoc.childNodes && xmlDoc.childNodes.length > 0) {
        var rootNode = xmlDoc.childNodes[0];
        if (rootNode.nodeType == 1) {
            root = makeChildNode(rootNode);
        }
    }
    return root;


}

function createLinearPath(points) {
    if (points == null)
        points = [];
    var l = points.length;
    var path = "";
    for (var i = 0; i < l; i++) {
        path += " " +points[i].x + " " + points[i].y;
    }
    var attrs = new Object();
    attrs.d = "M" + path;

    return makeSVG("path", attrs);
}